package com.novellotus.iov.carvice.model;

import android.widget.EditText;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.novellotus.iov.carvice.activity.SearchCarActivity;
import com.novellotus.iov.carvice.adapter.CarListAdapter;
import com.novellotus.iov.carvice.bean.RxCarListBean;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Rich on 2017/8/18.
 */

public class SearchCarModel {

    private SearchCarActivity context;
    private final int PAGE_SIZE = 10;

    public SearchCarModel(SearchCarActivity context) {
        this.context = context;
    }

    /**
     * @param editText 编辑框监听
     */
//    public void setSearchEditText(EditText editText) {
//        RxTextView.textChanges(editText).compose(RxSchedulers.<CharSequence>lifecycle(context)).subscribe(new Consumer<CharSequence>() {
//            @Override
//            public void accept(CharSequence charSequence) throws Exception {
//                if (charSequence.length() > 0) {
//                    searchInterface.searchCar();
//                }
//            }
//        });
//    }


    /**
     * 获取车辆列表
     */
    public void getList(EditText editText, final PageView pageView, final CarListAdapter carListAdapter) {
        Observable<BaseEntity<RxCarListBean>> observable = RetrofitFactory.getInstance().followVehicleList(SharedPreferencesUtils.getKey(context)[0], pageView.getLoadCurrentPage(), PAGE_SIZE, editText.getText().toString());
        observable.compose(RxSchedulers.<BaseEntity<RxCarListBean>>lifecycle(context))
                .compose(RxSchedulers.<BaseEntity<RxCarListBean>>compose())
                .subscribe(new BaseObserver<RxCarListBean>(context) {
                    @Override
                    public void onSuccess(RxCarListBean response) {
                        pageView.loadSuccess();
                        if (response != null) {
                            if (response.getRecords() == null || response.getRecords().size() == 0) {
                                pageView.loadFail("无相关数据", false);
                            }
                            pageView.setTotalPage(response.getTotal(), PAGE_SIZE);
                            if (response.getCurrent() == 1) {
                                carListAdapter.update(response.getRecords());

                            } else {
                                carListAdapter.addData(response.getRecords());
                            }
                        }


                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.loadFail(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.loadFail(msg, true);
                    }

                });
    }


}
